package dao

import (
	"context"
	"sale-admin/internal/models"

	"192.168.1.75/go-pkg/errorx"
	"gorm.io/gorm"
	"gorm.io/gorm/clause"
)

type OrderShippingTracking struct{}

func (OrderShippingTracking) Create(
	ctx context.Context,
	db *gorm.DB,
	record models.OrderShippingTracking,
) (id string, err error) {
	rowsAffected := db.WithContext(ctx).
		Model(&models.OrderShippingTracking{}).
		Clauses(clause.OnConflict{
			Columns:   []clause.Column{{Name: "out_trade_no"}},
			DoUpdates: clause.AssignmentColumns([]string{"data"}),
		}).
		Create(&record).RowsAffected
	if rowsAffected == 1 {
		return record.ID, nil
	}
	return "", errorx.New("新增轨迹失败", -1)
}

type KD100TrackData struct {
	Time       string `json:"time"`
	Context    string `json:"context"`
	Ftime      string `json:"ftime"`
	AreaCode   string `json:"areaCode"`
	AreaName   string `json:"areaName"`
	Status     string `json:"status"`
	Location   string `json:"location"`
	AreaCenter string `json:"areaCenter"`
	AreaPinYin string `json:"areaPinYin"`
	StatusCode string `json:"statusCode"`
}

func (OrderShippingTracking) List(
	ctx context.Context,
	db *gorm.DB,
	outTradeNO string,
) models.OrderShippingTracking {
	var record models.OrderShippingTracking
	db.WithContext(ctx).Model(&models.OrderShippingTracking{}).
		Where("out_trade_no = ?", outTradeNO).
		Take(&record)
	return record
}
